﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mobile.Models;
using System.Data.SqlClient;
using System.Configuration;

namespace Mobile.Controllers
{
    public class GuestUIController : Controller
    {
       
        // GET: /GuestUI./

        public ActionResult Index()
        {
            return View();
        }

        /* RefreshQueue PartialView Update
         * Desc: This should update all data Models with information from Mikahil's Webservice
         * 
         * @return PartialViewResult _Playlist.cshtml (in shared folder)
         */
        // GET: /GuestUI./RefreshQueue?partyID="3231"
        public PartialViewResult GetQueue(string partyID)
        {
            if (partyID == null) partyID = "fake_party_id";
            MainService.Service s = new MainService.Service();
            List<MainService.QueueData> QueueDataList = s.GetQueue(partyID).ToList();

            List<Song> songlist = new List<Song>();    

            foreach (MainService.QueueData qd in QueueDataList)
            {
                songlist.Add(new Song { ID = qd.Song.SongID, Title = qd.Song.Title, Artist = qd.Song.Artist, SongData = qd });
            }
            if (songlist.Count == 0) { return PartialView(); }
            Song nowplaying = songlist.ElementAt(0);
            songlist.RemoveAt(0);
            var queue = new Queue { partyID = partyID, queue = songlist, nowPlaying = nowplaying }; 

            return PartialView("_Playlist", queue);
        }


        /* AddSong()
         *  
         * Desc: This action is called to return the view of the AddSong Page
         *      the list of songs will be returned in a partial view 
         *      
         * @return AddSong View
         */
        // GET: /GuestUI./AddSong?partyID="3231"
        public ActionResult AddSong(string partyID)
        {
            if (partyID == null || partyID.Equals(""))
            {
                partyID = "fake_party_id";
            }
            MainService.Service s = new MainService.Service();
            List<MainService.SongData> songlist = s.GetAllSongs(partyID).ToList();
            Party party = new Party { PartyID = partyID, allSongs = songlist };

            return View(party);
        }


        /* Playlist View ActionResult
         * 
         * Desc: This is view is returned by the controller the first time a user navigates from 
         *      another view to the Playlist. Will check/get party ID and update queue each time
         * 
         * @return Playlist View
         */
        // 
        public ActionResult Playlist(FormCollection collection)
        {
            string partyCode = collection.Get("partyCode");
                string partyID = null;
                if (partyCode != null)
                {
                    partyID = GetPartyID(partyCode);
                }
                if (partyID == null)
                {
                    //default partyID to our test party in db -- this is for testing
                    partyID = "fake_party_id";
                }
                MainService.Service s = new MainService.Service();
                List<MainService.QueueData> QueueDataList = s.GetQueue(partyID).ToList();

                List<Song> songlist = new List<Song>();
                songlist.Add(new Song { ID = "1", Title = "Sample Song", Artist = "Sample Artist", SongData = null });
                foreach (MainService.QueueData qd in QueueDataList)
                {
                    
                    songlist.Add(new Song { ID = qd.Song.SongID, Title = qd.Song.Title, Artist = qd.Song.Artist, SongData = qd });
                }
                if (QueueDataList.Count() == 0)
                {
                    songlist.Add(new Song { ID = "1", Title = "Sample Song", Artist = "Sample Artist", SongData = null });
                }   
                try
                {

                    var queue = new Queue { partyID = partyID, queue = songlist, nowPlaying = songlist.ElementAt(0) };
                    return View(queue);
                }
                catch
                {
                }
            return View();
        }


        private string GetPartyID(string accessCode)
        {
            SqlDataReader reader;
            try
            {
                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NextrackConn"].ConnectionString);
                myConnection.Open();

                //Get ID
                using (SqlCommand command = new SqlCommand("SELECT * FROM Parties Where Access_Code=@Access_Code", myConnection))
                {
                    command.Parameters.Add(new SqlParameter("Access_Code", accessCode));
                    reader = command.ExecuteReader();

                    reader.Read();
                    return reader["Party_ID"].ToString();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /* UpvoteButton Action Method
         * 
         * Desc: Takes Song ID from ActionLink call and tells service to upvote
         * 
         * @param string SongID: the songID that has been updated
         * @return: partial view Queue
         */ 
        public ActionResult UpvoteButton(string songID, string partyID)
        {
             if (partyID == null) partyID = "123";
             

            MainService.Service s = new MainService.Service();
            if (partyID != null && songID != null) { s.UpvoteSong(partyID, songID); }

            List<MainService.QueueData> QueueDataList = s.GetQueue(partyID).ToList();
            List<Song> songlist = new List<Song>();    

            foreach (MainService.QueueData qd in QueueDataList)
            {
                songlist.Add(new Song { ID = qd.Song.SongID, Title = qd.Song.Title, Artist = qd.Song.Artist, SongData = qd });
            }
            if (songlist.Count == 0) { return PartialView(); }
            Song nowplaying = songlist.ElementAt(0);
            songlist.RemoveAt(0);
            var queue = new Queue { partyID = partyID, queue = songlist, nowPlaying = nowplaying };

            return View("Playlist", queue);
        }

        //private void DownvoteButton(string songid)
        //{

        //}

    }

}
